package com.cpos.cposcommon.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Logger;

/*
 *  @创建人  Leiwenlong
 *  @创建时间 2020/11/29
 *  @描述  Access Token拦截器
 */

@Component
@Slf4j
public class AccessTokenVerifyInterceptor extends HandlerInterceptorAdapter {

     /* @Autowired
        ValidationService validationService;
     */

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            log.info("AccessToken executing ...");
            boolean flag = false;
            // token
            String accessToken = request.getParameter("token");
            if (!StringUtils.isEmpty(accessToken)) {
                // 验证
//                ValidationModel v = validationService.verifyAccessToken(accessToken);
                // 时间过期
                // 用户验证
/*                if (v != null) {
                   User user = userService.findById(v.getUid());
                   if(user != null) {
                        request.setAttribute(CommonConst.PARAM_USER, user);
                       LOG.info("AccessToken SUCCESS ...  user:" + user.getUserName() + " - " + accessToken);
                      flag = true;
                    }
                }*/
            }
            if (!flag) {
                response.setStatus(HttpStatus.FORBIDDEN.value());
                response.getWriter().print("AccessToken ERROR");
            }
            return flag;
        }
}
